<?php
namespace Index\Controller;
use Think\Controller;
use Think\Page;
class PeopleController extends Controller {
    public function index(){
        if(!IS_POST){
            $id = I("get.id");
            $this->id = $id;
            $m =M();
            $sql = "select * from user where id ={$id}";
            $user = $m->query($sql);
            $user = $user[0];
            $sql = "select t1.*,t2.* from guanzhu t1 left join user t2 on t1.gid = t2.id where t1.uid ={$id}";
            $gz =  $m->query($sql);
            $gzs = count($gz);
            $sql = "select t1.*,t2.* from guanzhu t1 left join user t2 on t1.uid = t2.id where t1.gid = {$id};";
            $bgz = $m->query($sql);
            $bgzs = count($bgz);
            $sql = "select * from guanzhu where uid = {$_SESSION['id']} and gid = {$id}";
            $ret = $m->query($sql);
            if(empty($ret)){
                $gzpd = 0;
            }else{
                $gzpd = 1;
            }
            if($id == $_SESSION['id']){
                $pd = 0;
            }else{
                $pd = 1;
            }
            //取回复sql
            $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc limit 5";
            $huifu = $m->query($sql);
            $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc";
            $huifu_gs = count($m->query($sql));
            $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc limit 10";
            $Xhuifu = $m->query($sql);
            $sql = "select * from wenti where sid = {$id} order by tim desc limit 5";
            $fawen = $m->query($sql);
            $fwgs = count($fawen);
            $this->assign("user",$user);
            $this->assign("fawen",$fawen);
            $this->assign("fwgs",$fwgs);
            $this->assign("id",$id);
            $this->assign("gz",$gz);
            $this->assign("gzs",$gzs);
            $this->assign("pd",$pd);
            $this->assign("bgz",$bgz);
            $this->assign("bgzs",$bgzs);
            $this->assign("gzpd",$gzpd);
            $this->assign("huifu",$huifu);
            $this->assign("huifu_gs",$huifu_gs);
            $this->assign("Xhuifu",$Xhuifu);
            $this->display('index');
            var_dump($gz);
            var_dump($bgz);
        }else{
            $m = M();
            $uid = I("post.uid");
            $gid = I("post.gid");
            $sql = "insert into guanzhu(uid,gid) values('$uid','$gid')";
            if($m->execute($sql)==0){
                echo 1;
            }else{
                echo 0;
            }
        }

    }
    public function hehe($h){
        $arr = array();
        foreach($h as $k => $v){
            if(is_array($v)){
                $arr[$k] = $this->hehe($v);
            }else{
                $arr[$k] = urlencode($v);
            }
        }
        return $arr;
    }
//    public function chagengduo(){
//        $id = I("post.id");
//        $gs = I("post.gs");
//        $m = M();
//        $xgs = $gs+10;
//        $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc";
//        $huifu_gs = count($m->query($sql));
//        if($xgs > $huifu_gs){
//            $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc limit {$gs},{$this->huifu_gs}";
//        }else{
//            $sql = "select a.text,b.text text_hf,b.wid from wenti a left join huifu b on a.id = b.wid where b.uid = {$id} order by b.tim desc limit {$gs},{$xgs}";
//        }
//        $ret = $this->hehe($m->query($sql));
//        echo urldecode (json_encode($ret));
//
//    }
}